package com.phamkhanh.common;

public class Chromo {

    private static int numGene;
    private static float[] upper;
    private static float[] lower;
    private float[] genes;
    private static Fitness fitness;

    public Chromo() {
        boolean repeated = true;
        while(repeated){
            
            genes = new float[numGene];
            for (int i = 0; i < numGene; i++) {
                genes[i] = (float) (lower[i] + Math.random()*(upper[i] - lower[i]));
            }
            
            if(value() != 0) repeated = false;
        }
        
    }

    public static float[] getLower() {
        return lower;
    }

    public static void setLower(float[] lower) {
        Chromo.lower = lower;
    }

    public static int getNumGene() {
        return numGene;
    }

    public static void setNumGene(int numGene) {
        Chromo.numGene = numGene;
    }

    public static float[] getUpper() {
        return upper;
    }

    public static void setUpper(float[] upper) {
        Chromo.upper = upper;
    }

    public static Fitness getFitness() {
        return fitness;
    }

    public static void setFitness(Fitness fitness) {
        Chromo.fitness = fitness;
    }

    public float getGene(int i) {
        return this.genes[i];
    }

    public void setGene(int i, float value) {
        this.genes[i] = value;
    }

    public float[] getGenes() {
        return genes;
    }

    public void setGenes(float[] genes) {
        this.genes = genes;
    }
    
    

    // Do thich nghi cua ca the thuoc (0,1] cang gan 1 thi cang thich nghi, tri tuyet doi max cang nho
    public float value() {
        return fitness.calculate(genes);
    }
    
    public float maxAbs(){
        return 1/value() - 1;
    }

    @Override
    public Chromo clone() {
        Chromo result = new Chromo();
        System.arraycopy(this.genes, 0, result.genes, 0, genes.length);
        return result;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append("(");
        for(int  i = 0; i < numGene; i++) result.append(genes[i]).append(",");
        result.append(")").append(value());
        return result.toString();
    }

}
